package ysq.xyg.host.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.base.model.dto.ReturnEntity;
import ysq.xyg.base.service.impl.BaseServiceImpl;
import ysq.xyg.base.util.BaseConstant;
import ysq.xyg.base.util.BaseUtil;
import ysq.xyg.host.dao.HostCollectDao;
import ysq.xyg.host.model.dto.HostItemDto;
import ysq.xyg.host.model.po.HostCollect;
import ysq.xyg.host.model.vo.HostCollectVo;
import ysq.xyg.host.service.HostCollectService;
import ysq.xyg.sys.model.po.SysUser;

import java.util.Date;
import java.util.List;

/**
 * 房产-楼盘收藏service实现
 *
 * @author congcong tao
 * @date 2019-12-11 15:22:24
 */
@Service("hostCollectService")
public class HostCollectServiceImpl extends BaseServiceImpl<HostCollect> implements HostCollectService {

    @Autowired
    private HostCollectDao hostCollectDao;

    @Autowired
    public void setBaseDao(HostCollectDao hostCollectDao) {
        this.baseDao = hostCollectDao;
    }

    /**
     * 分页查询
     *
     * @param hostCollectVo 对象
     * @param pageSize 展示数量
     * @param pageNum 当前页面
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:22:24
     */
    @Override
    public ReturnEntity page(HostCollectVo hostCollectVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        PageHelper.orderBy(" update_time desc");
        List<HostCollect> list = acceptList(hostCollectVo);
        PageInfo<HostCollect> page = new PageInfo<HostCollect>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    @Override
    public ReturnEntity myCollectHostItemPage(HostCollectVo hostCollectVo, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        List<HostItemDto> list = hostCollectDao.selectMyCollectHostItemList(hostCollectVo);
        PageInfo<HostItemDto> page = new PageInfo<>(list);
        return new ReturnEntity(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 修改
     *
     * @param hostCollectVo 对象
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:22:24
     */
    @Override
    public ReturnEntity addOrUpdate(HostCollectVo hostCollectVo, SysUser currentUser) {
        boolean isUpate = true;
        HostCollect hostCollect;
        if (StrUtil.isEmpty(hostCollectVo.getId())) {
            hostCollect = new HostCollect(currentUser);
            isUpate = false;
        } else {
            hostCollect = selectByPrimaryKey(hostCollectVo.getId());
            if (ObjectUtil.isNull(hostCollect)) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            hostCollect.setUpdateTime(new Date());
        }
        hostCollect.setItemId(hostCollectVo.getItemId());
        hostCollect.setMemberId(hostCollectVo.getMemberId());
        int num = 0;
        if (isUpate) {
            num = updateByPrimaryKey(hostCollect);
        } else {
            num = insert(hostCollect);
        }
        if (num <= 0) {
            return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
        }
        return new ReturnEntity();
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @param currentUser 当前用户
     * @return ReturnEntity 对象
     * @author congcong tao
     * @date 2019-12-11 15:22:24
     */
    @Override
    public ReturnEntity delete(String id, SysUser currentUser) {
        HostCollect hostCollect = selectByPrimaryKey(id);
        if (hostCollect != null) {
            hostCollect.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_DELETE);
            hostCollect.setUpdateTime(new Date());
            int num = updateByPrimaryKey(hostCollect);
            if (num > 0) {
                return new ReturnEntity();
            }
        }
        return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
    }

    /**
     * 获取集合
     *
     * @param hostCollectVo 对象
     * @return List<BaseDict> BaseDict集合
     * @author congcong tao
     * @date 2019-12-11 15:22:24
     */
    @Override
    public List<HostCollect> acceptList(HostCollectVo hostCollectVo){
        Example example = new Example(HostCollect.class);
        Example.Criteria criteria = example.createCriteria();
        if (StringUtil.isNotEmpty(hostCollectVo.getRemoveTag())) {
            criteria.andEqualTo("removeTag", hostCollectVo.getRemoveTag());
        }
        if (StringUtil.isNotEmpty(hostCollectVo.getMemberId())) {
            criteria.andEqualTo("memberId", hostCollectVo.getMemberId());
        }
        if (StringUtil.isNotEmpty(hostCollectVo.getItemId())) {
            criteria.andEqualTo("itemId", hostCollectVo.getItemId());
        }
        example.setOrderByClause(" update_time desc");
        return hostCollectDao.selectByExample(example);
    }

    @Override
    public ReturnEntity saveHostCollect(HostCollectVo hostCollectVo) {
        // 判断是否已收藏过
        HostCollectVo hostCollectVoS = new HostCollectVo();
        hostCollectVoS.setMemberId(hostCollectVo.getMemberId());
        hostCollectVoS.setItemId(hostCollectVo.getItemId());
        List<HostCollect> hostCollects = acceptList(hostCollectVoS);
        HostCollect hostCollect;
        if (CollUtil.isNotEmpty(hostCollects)) {
            hostCollect = hostCollects.get(0);
            // 删除
            int num = this.deleteByPrimaryKey(hostCollect.getId());
            if (num <= 0) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            return new ReturnEntity("2");
        } else {
            hostCollect = new HostCollect();
            hostCollect.setId(BaseUtil.uId());
            hostCollect.setRemoveTag(BaseConstant.BASE_REMOVE_TAG_NORMAL);
            hostCollect.setCreateTime(new Date());
            hostCollect.setMemberId(hostCollectVo.getMemberId());
            hostCollect.setItemId(hostCollectVo.getItemId());
            hostCollect.setUpdateTime(hostCollect.getCreateTime());
            int num = insert(hostCollect);
            if (num <= 0) {
                return new ReturnEntity(BaseConstant.FAIL, BaseConstant.DO_FAIL);
            }
            return new ReturnEntity("1");
        }
    }

    @Override
    public boolean booleanCollect(String memberId, String itemId) {
        HostCollectVo hostCollectVo = new HostCollectVo();
        hostCollectVo.setMemberId(memberId);
        hostCollectVo.setItemId(itemId);
        List<HostCollect> hostCollects = acceptList(hostCollectVo);
        if (CollUtil.isNotEmpty(hostCollects)) {
            return true;
        }
        return false;
    }
}
